<template>
  <div class="dialog">
    <el-dialog
      :title="{ dialog: '新增角色', lock: '修改角色' }[mode]"
      :visible="isShow"
      @close="closeEvent"
    >
      <el-form ref="form" label-width="100px" :model="form" :rules="rules">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="form.description" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: center">
          <el-button @click="closeEvent">取消</el-button>
          <el-button type="primary" @click="submit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { sysRolePost, sysRolePut } from '@/api/setting.js'
export default {
  model: {
    prop: 'isShow',
    event: 'input'
  },
  props: {
    isShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      mode: 'dialog',
      form: {
        name: '', //	string	必须		角色名称
        description: '' //	string	必须		角色描述
      },
      rules: {
        name: { required: true, message: '必填项', trigger: 'change' },
        description: { required: true, message: '必填项', trigger: 'change' }
      }
    }
  },
  methods: {
    closeEvent() {
      //   console.log('closeEvent')
      //   关闭弹窗
      this.$emit('input', false)
      //   清空表单数据与验证
      this.$refs.form.resetFields()
      this.form = {
        name: '',
        description: ''
      }
    },
    submit() {
      // 全局验证
      this.$refs.form.validate(async(result) => {
        if (result) {
          if (this.mode === 'dialog') {
            await sysRolePost(this.form)
            //   提示用户
            this.$message.success('添加成功')
          } else if (this.mode === 'lock') {
            await sysRolePut(this.form)
            this.$message.success('修改成功')
          }
          this.closeEvent()
          //   刷新父级列表
          this.$emit('getData')
        }
      })
    }
  }
}
</script>

<style></style>
